//
//  EditAddressViewController.m
//  EasyToTesco
//
//  Created by mc on 16/2/23.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "EditAddressViewController.h"
#import "HeaderFileForEasyToTesco.h"

#import "AnotherEditViewController.h"
#import "EditAddressTableViewCell.h"

@interface EditAddressViewController ()<UITableViewDataSource, UITableViewDelegate>{
    NSArray *firstArray;
    NSArray *secondArray;
}


@property(nonatomic, strong)UITableView *editAddressTableview;
@end

@implementation EditAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self addEditAddTopView];

    [self addEditaddrTableview];
    
    firstArray = @[@"收货人: ", @"手机号码: ", @"所在地区: ", @"邮政编码: ", @"街道地址: "];
    secondArray = @[@"沉甸甸", @"12345678900", @"浙江省杭州市西湖区", @"310013", @"四季青"];
    
}

- (void)addEditAddTopView {
    
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, (self.secondView.frame.size.height-20)/2, 20, 20)];
    image1.image = [UIImage imageNamed:@"goback-icon.png"];
    [self.secondView addSubview:image1];
    self.leftBtn.hidden = YES;
    
    UIButton *goBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackBtn.frame = CGRectMake(0, 0, self.secondView.frame.size.height, self.secondView.frame.size.height);
    [goBackBtn addTarget:self action:@selector(gobackClick) forControlEvents:UIControlEventTouchUpInside];
    [self.secondView addSubview:goBackBtn];
    self.titleLabel.text = @"编辑收货地址";

}

- (void)gobackClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)addEditaddrTableview{
    self.editAddressTableview = [[UITableView alloc] initWithFrame:CGRectMake(0, HIGHT*0.1, WEITH, HIGHT*0.9) style:UITableViewStylePlain];
    self.editAddressTableview.dataSource = self;
    self.editAddressTableview.delegate = self;
    self.editAddressTableview.scrollEnabled = NO;
    self.editAddressTableview.backgroundColor = [UIColor colorWithRed:244/255. green:244/255. blue:244/255. alpha:1];
    [self.view addSubview:self.editAddressTableview];
}


#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 5;
    }else {
        return 1;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    
    if (indexPath.section == 0) {
        static NSString *identifier = @"EditAddressTableViewCell";
        EditAddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"EditAddressTableViewCell" owner:nil options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.TitleLabel.text = firstArray[indexPath.row];
        cell.introLabel.text = secondArray[indexPath.row];
                return cell;
    } else {
        static NSString *identifier = @"editAddCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        cell.textLabel.textColor = [UIColor colorWithRed:52/255. green:147/255. blue:225/255. alpha:1];
        cell.textLabel.text = @"删除地址";
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        return cell;
    }
    

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return 10;
    } else {
        return 80;
    }
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footView = [[UIView alloc] init];
    footView.backgroundColor = [UIColor colorWithRed:244/255. green:244/255. blue:244/255. alpha:1];
    if (section == 1) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 200, 20)];
        label.font = [UIFont systemFontOfSize:12];
        label.text = @"当前地址已经是默认地址";
        label.textColor = [UIColor grayColor];
        [footView addSubview:label];
        
        UIButton *SaveBtn =[ UIButton buttonWithType:UIButtonTypeCustom];
        SaveBtn.frame = CGRectMake(40, CGRectGetMaxY(label.frame)+15, WEITH-80, 40);
        SaveBtn.backgroundColor = [UIColor colorWithRed:52/255. green:147/255. blue:225/255. alpha:1];
        [SaveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [SaveBtn setTitle:@"保存并使用地址" forState:UIControlStateNormal];
        SaveBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        SaveBtn.layer.cornerRadius = 6;
        SaveBtn.layer.masksToBounds = YES;
        [SaveBtn addTarget:self action:@selector(saveBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [footView addSubview:SaveBtn];
    }
    return footView;
}


- (void)saveBtnClick {
    NSLog(@"保存");
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    AnotherEditViewController *anotherVC = [[AnotherEditViewController alloc] init];
//    [self presentViewController:anotherVC animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
